package com.shareach.util;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;

import com.benayn.ustyle.logger.Loggers;

public class DateUtils extends PropertyEditorSupport{
	private static final Log log = Loggers.from(DateUtils.class);
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	private String dateFormat="yyyy-MM-dd";
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		log.info("set Text");
		SimpleDateFormat frm = new SimpleDateFormat(dateFormat);
		try{
			Date date = frm.parse(text);
			this.setValue(date);
		}catch(Exception exp)
		{
			exp.printStackTrace();
		}
		
	}
	
	public static Date parse(String text) throws ParseException{
		String dateFormat="yyyy-MM-dd";
		SimpleDateFormat frm = new SimpleDateFormat(dateFormat);
		return frm.parse(text);
	}

	public static String parse(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
		return format.format(date);
	}
	public static String parse(Date date, String formatString) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		return format.format(date);
	}
	
	/**
	 * 指定时间的凌晨
	 * @param date
	 * @return
	 */
	public static Date getDateZero(Date date){
		date = org.apache.commons.lang.time.DateUtils.setHours(date, 0);
		date = org.apache.commons.lang.time.DateUtils.setMinutes(date, 0);
		date = org.apache.commons.lang.time.DateUtils.setSeconds(date, 0);
		date = org.apache.commons.lang.time.DateUtils.setMilliseconds(date, 0);
		return date;
	}
	public static Calendar getDateZeroCalendar(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal;
	}
	
	public static Date add(Date date,int zoom, int amount){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(zoom, amount);
		return cal.getTime();
	}
	
	public static void main(String[] args) {
		Date orderDate = getDate("2012-09-24 17:32:00");
		log.debug(getDate(orderDate));
		
		Date now = new Date();
		log.debug(getDate(now));
		
		Date ta = org.apache.commons.lang.time.DateUtils.addMinutes(now, -30);
		log.debug(getDate(ta));
		
		log.debug(orderDate.before(ta));
	}
	
	public static Date getDate(String date) {
		return getDate(date, DATE_TIME_FORMAT);
	}
	
	public static Date getDate(String date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		try {
			return df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getDate(Date date) {
		return getDate(date, DATE_TIME_FORMAT);
	}
	
	public static String getDate(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
}
